package http_proxy_router

import (
	"gateway/controller"
	"gateway/http_proxy_middleware"

	"github.com/gin-gonic/gin"
)

func InitRouter(middlewares ...gin.HandlerFunc) *gin.Engine {
	//todo 优化点1
	//router := gin.Default() // 默认打印请求日志
	router := gin.New() // 不打印请求日志
	router.Use(middlewares...)

	oauth := router.Group("/oauth")
	oauth.Use()
	{
		controller.OAuthRegister(oauth)
	}

	api := router.Group("/api")
	api.Use(
		http_proxy_middleware.HTTPAccessModeMiddleware(),

		http_proxy_middleware.HTTPFlowCountMiddleware(),
		http_proxy_middleware.HTTPFlowLimitMiddleware(),

		http_proxy_middleware.HTTPJwtAuthTokenMiddleware(),
		http_proxy_middleware.HTTPJwtFlowCountMiddleware(),
		http_proxy_middleware.HTTPJwtFlowLimitMiddleware(),

		http_proxy_middleware.HTTPWhiteListMiddleware(),
		http_proxy_middleware.HTTPBlackListMiddleware(),

		http_proxy_middleware.HTTPHeaderTransferMiddleware(),
		http_proxy_middleware.HTTPStripUriMiddleware(),
		http_proxy_middleware.HTTPUrlRewriteMiddleware(),

		http_proxy_middleware.HTTPReverseProxyMiddleware(),
	)
	{
		api.Any("/*any", func(c *gin.Context) {
			c.String(404, "server not found")
		})
	}

	return router
}
